package ecom.appcode.web.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ecom.appcode.business.CategoryLogic;
import ecom.appcode.business.CategoryLogicImpl;
import ecom.appcode.business.ShoesLogic;
import ecom.appcode.business.ShoesLogicImpl;
import ecom.appcode.business.UsersLogic;
import ecom.appcode.business.UsersLogicImpl;
import ecom.appcode.common.constants.CommonConstants;
import ecom.appcode.common.daobean.CategoryDaoBean;
import ecom.appcode.common.daobean.ShoesDaoBean;
import ecom.appcode.common.daobean.UsersDaoBean;

/**
 * Servlet implementation class Administrator
 */
public class Administrator extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final String product_management = "product-management.jsp";
    private static final String edit_shoes = "edit-shoes.jsp";
    private static final String users_management = "users-management.jsp";
    private static final String edit_users = "edit-users.jsp";

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Administrator() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String action = request.getParameter("btAction");
        if ("loadShoes".equals(action)) {
            ShoesLogic shoesLogic = new ShoesLogicImpl();
            List<ShoesDaoBean> shoesList = shoesLogic.loadAllShoes();
            if (shoesList != null) {
                HttpSession session = request.getSession();
                session.setAttribute("LOADALLSHOES", shoesList);
                RequestDispatcher rd = request.getRequestDispatcher(product_management);
                rd.forward(request, response);
            }
        } else if ("editShoe".equals(action)) {
            String strShoesID = request.getParameter("id");
            int shoeID = Integer.parseInt(strShoesID);
            /** Load a Shoes */
            ShoesLogic shoesLogic = new ShoesLogicImpl();
            ShoesDaoBean shoe = shoesLogic.loadAShoesProduct(shoeID);
            /** Load Category */
            CategoryLogic cateLogic = new CategoryLogicImpl();
            List<CategoryDaoBean> cateList = cateLogic.loadCategoryName();
            if (shoe != null) {
                HttpSession session = request.getSession();
                session.setAttribute("SHOE", shoe);
                session.setAttribute(CommonConstants.SESSION_CATEGORY_LIST, cateList);
                RequestDispatcher rd = request.getRequestDispatcher(edit_shoes);
                rd.forward(request, response);
            }
        } else if ("loadUsers".equals(action)) {
            UsersLogic usersLogic = new UsersLogicImpl();
            List<UsersDaoBean> usersList = usersLogic.loadAllUsers();
            if (usersList != null) {
                HttpSession session = request.getSession();
                session.setAttribute("LOADALLUSERS", usersList);
                RequestDispatcher rd = request.getRequestDispatcher(users_management);
                rd.forward(request, response);
            }
        } else if ("editUser".equals(action)) {
            String email = request.getParameter("email");

            UsersLogic usersLogic = new UsersLogicImpl();
            UsersDaoBean user = usersLogic.loadUsersByEmail(email);
            if (user != null) {
                HttpSession session = request.getSession();
                session.setAttribute("USER", user);
                RequestDispatcher rd = request.getRequestDispatcher(edit_users);
                rd.forward(request, response);
            }
        } else if ("ViewUser".equals(action)) {
            String email = request.getParameter("email");

            UsersLogic usersLogic = new UsersLogicImpl();
            UsersDaoBean user = usersLogic.loadUsersByEmail(email);
            if (user != null) {
                HttpSession session = request.getSession();
                session.setAttribute("USER", user);
                RequestDispatcher rd = request.getRequestDispatcher("view-user-order.jsp");
                rd.forward(request, response);
            }
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // TODO Auto-generated method stub
    }

}
